package com.zjlanyun.erp.customer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zjlanyun.erp.Common;
import com.zjlanyun.erp.Data;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.utils.Toastor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.HashMap;

import cz.msebera.android.httpclient.Header;

/**
 * @author:shun
 * @date:2016年2月18日 上午10:31:56
 * @version:V_1.0
 * @Description:
 */

public class CustomerDetail extends Activity {

	private TextView title;
	private LinearLayout ll_back;
	private Context mContext = this;
	private LinearLayout mainContent;
	private HashMap<String, Object> userinfo;
	private Toastor toastor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_detail);
		initView();
	}

	// 初始化View
	private void initView() {
		title = (TextView) findViewById(R.id.title);
		ll_back = (LinearLayout) findViewById(R.id.ll_back);
		mainContent = (LinearLayout) findViewById(R.id.content);

		title.setText("客户详情");
		userinfo = Common.getUserinfo(mContext);
		toastor = new Toastor();

		if((Boolean) userinfo.get("isDemo")){
			initDemoData();
		}else{
			getApiData();
		}
		
		// 后退
		ll_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	// 获取网络数据
	private void getApiData(){
		Intent intent = getIntent();
		String serverUrl = WebApi.customer(mContext);
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("username", userinfo.get("username"));
		params.put("password", userinfo.get("password"));
		params.put("type", "detail");
		params.put("FItemID", intent.getIntExtra("FItemID", 0));
		client.post(serverUrl, params, new TextHttpResponseHandler() {
			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				toastor.showShortToast(Data.SERVER_URL_ERROR);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				JSONTokener jsonParser = new JSONTokener(responseString);
				try {
					JSONObject json = (JSONObject) jsonParser.nextValue();
					if(json.getInt("code") == 0){
						JSONArray data = json.getJSONArray("data");
						for(int i = 0; i < data.length(); i++){
							JSONObject item = data.getJSONObject(i);
							LayoutInflater layoutInflater = LayoutInflater.from(mContext);
							View view = layoutInflater.inflate(R.layout.customer_detail_item, null);

							TextView title = (TextView) view.findViewById(R.id.title);
							title.setText(item.getString("name"));
							LinearLayout content = (LinearLayout) view.findViewById(R.id.content);

							JSONArray list = item.getJSONArray("content");

							for(int j = 0; j < list.length(); j++){
								LayoutInflater layoutInflater_item = layoutInflater.from(mContext);
								View view_item = layoutInflater_item.inflate(R.layout.customer_detail_item_item, null);

								TextView tv_item = (TextView) view_item.findViewById(R.id.item);
								JSONObject itemContent = list.getJSONObject(j);
								tv_item.setText(itemContent.getString("name") + " : " + itemContent.getString("value"));
								content.addView(view_item);
							}
							mainContent.addView(view);
						}
					}else{
						toastor.showShortToast(json.getString("message"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}

	// 初始化测试数据
	private void initDemoData() {
		String[] titles = { "客户基本信息状况", "经营状况", "信用状况", "与我方往来", "其他" };
		ArrayList<ArrayList<HashMap<String, Object>>> mList = new ArrayList<ArrayList<HashMap<String, Object>>>();

		String[] names = { "公司", "负责人", "地址", "联系方式", "订货人", "订货人联系方式", "机器（数量、类型）", "车辆", "店面面积", "员工数量", "行业从业时间",
				"与我方接洽员工" };
		String[] values = { "浙江蓝云信息科技股份有限公司", "总经理", "浙江嘉善县科创中心", "13812345678", "郭", "13612345678", "", "", "200平米",
				"120", "10年", "" };
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < names.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", names[i]);
			map.put("value", values[i]);
			list.add(map);
		}
		mList.add(list);

		names = new String[] { "类型", "主要产品需求", "主要供应商", "主要收入来源" };
		values = new String[] { "个体", "材料", "浙江蓝云", "销售产品" };
		list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < names.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", names[i]);
			map.put("value", values[i]);
			list.add(map);
		}
		mList.add(list);

		names = new String[] { "给定款期", "通常结款方式", "付款态度", "违约记录", "以前与我方或其他供应商停止合作原因" };
		values = new String[] { "", "转账", "积极", "无", "质量问题" };
		list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < names.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", names[i]);
			map.put("value", values[i]);
			list.add(map);
		}
		mList.add(list);

		names = new String[] { "合作开始时间", "主要采购产品", "月均销量（千）", "与我方关系程度", "客户主要需求" };
		values = new String[] { "2016-02-01", "方向盘", "32433", "紧密合作", "价格、质量、款期、服务" };
		list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < names.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", names[i]);
			map.put("value", values[i]);
			list.add(map);
		}
		mList.add(list);

		names = new String[] { "客户当前重大风险事项", "客户（老板或者负责采购者）的爱好" };
		values = new String[] { "无", "无" };
		list = new ArrayList<HashMap<String, Object>>();
		for(int i = 0; i < names.length; i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", names[i]);
			map.put("value", values[i]);
			list.add(map);
		}
		mList.add(list);

		for(int i = 0; i < titles.length; i++){
			LayoutInflater layoutInflater = LayoutInflater.from(mContext);
			View view = layoutInflater.inflate(R.layout.customer_detail_item, null);
			
			TextView title = (TextView) view.findViewById(R.id.title);
			title.setText(titles[i]);
			LinearLayout content = (LinearLayout) view.findViewById(R.id.content);
			
			list = new ArrayList<HashMap<String, Object>>();
			list = mList.get(i);
			
			for(int j = 0; j < list.size(); j++){
				LayoutInflater layoutInflater_item = layoutInflater.from(mContext);
				View view_item = layoutInflater_item.inflate(R.layout.customer_detail_item_item, null);
				
				TextView item = (TextView) view_item.findViewById(R.id.item);
				item.setText(list.get(j).get("name").toString() + " : " + list.get(j).get("value").toString());
				content.addView(view_item);
			}
			mainContent.addView(view);
		}
	}
}
